<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Sp_mahasiswa extends zulController{
	
	function __construct(){
		parent::__construct();
		$this->load->model("sp_mahasiswa_model");
		// $this->load->model("master_kelas_model");
		$this->checkLogin();
	}
	public function index($id)
	{
		$colModel['NO'] = array('No', 20, TRUE, 'center', 0);
		$colModel['EDIT'] = array('Edit', 70, FALSE, 'center', 0);
		$colModel['NAMA'] = array('Nama Mahasiswa', 100, FALSE, 'center', 0);
		$colModel['TANGGAL_SP'] = array('Tanggal SP', 100, FALSE, 'center', 0);
		$colModel['KETERANGAN'] = array('Keterangan', 200, FALSE, 'center', 1);
        $gridParams = array(
            'width' => 'auto',
            'height' => '390',
            'rp' => 15,
            'rpOptions' => '[10,15,20,25,40]',
            'pagestat' => 'Displaying: {from} to {to} of {total} items.',
            'blockOpacity' => 0.5,
            'title' => 'Daftar SP Mahasiswa'
        );
        $buttons[] = array('Tambah', 'add', 'sp_mahasiswa');
        // $buttons[] = array('Hapus', 'delete', 'sp_mahasiswa');
        $buttons[] = array('separator');
		$buttons[] = array('Kembali', 'prev', 'sp_mahasiswa');
        // $buttons[] = array('Pilih Semua', 'add', 'sp_mahasiswa');
        // $buttons[] = array('Hapus Pilihan', 'delete', 'sp_mahasiswa');
        // $buttons[] = array('separator');
		//$buttons[] = array('Rekap Stok Obat', 'print', 'sp_mahasiswa');
		
        $grid_js = build_grid_js('flex1', site_url("sp_mahasiswa/grid_sp/" . $id), $colModel, 'ID_SP', 'ASC', $gridParams, $buttons);

        $data['title'] = "Daftar SP Mahasiswa";
        $data['info'] = "Daftar SP Mahasiswa";
        $data['js_grid'] = $grid_js;


        $data['added_js'] =
                "<script type='text/javascript'>
			function sp_mahasiswa(com,grid)
			{
				if (com=='Pilih Semua')
				{
					$('.bDiv tbody tr',grid).addClass('trSelected');
				}
				
				if (com=='Hapus Pilihan')
				{
					$('.bDiv tbody tr',grid).removeClass('trSelected');
				}
				if (com=='Tambah')
				{
					location.href='" . base_url() . "index.php/sp_mahasiswa/form_sp/" . $id . "'; 
				}
				if (com=='Kembali')
				{
					location.href='" . base_url() . "index.php/master_mahasiswa'; 
				}
				if (com=='Hapus')
				{
				   if($('.trSelected',grid).length>0){
					   if(confirm('Anda yakin ingin mengaktifkan ' + $('.trSelected',grid).length + ' buah data?')){
							var items = $('.trSelected',grid);
							var itemlist ='';
							for(i=0;i<items.length;i++){
								itemlist+= items[i].id.substr(3)+',';
							}
							$.ajax({
							   type: 'POST',
							   url: '" .base_url('index.php/sp_mahasiswa/delete_sp')."',
							   data: 'items='+itemlist,
							   success: function(data){
								$('#flex1').flexReload();
								alert(data);
							   }
							});
						}
					} else {
						return false;
					} 
				}
			} 
			</script>
			";
		$data["active"] = "master";
		$data["content"]	= $this->load->view("grid",$data,true);
		$this->load->view("main_new",$data);
	}
	function grid_sp($id){
		$valid_fields = array('ID_SP', 'NAMA', 'TANGGAL_SP', 'KETERANGAN');
        $this->flexigrid->validate_post('ID_SP', 'ASC', $valid_fields);
		$join	= array(
			array("table"=>"sp_mahasiswa","join_key"=>"ID_MAHASISWA","join_table"=>"master_mahasiswa","join_type"=>"left"),
		);
		$column	= array('ID_SP', 'NAMA', 'TANGGAL_SP', 'KETERANGAN');
        $records = $this->sp_mahasiswa_model->get_joinFG($join,array("sp_mahasiswa.ID_MAHASISWA"=>$id),$column);
        $this->output->set_header($this->config->item('json_header'));
        $no = 1;
        foreach ($records['records']->result() as $row) {
            $record_items[] = array($row->ID_SP,
                $no,
				"<a href='".site_url("/sp_mahasiswa/form_sp/" . $id . "/" . $row->ID_SP)."'><img src='". base_url()."assets/images/grid/edit.png'></a>",
				$row->NAMA,
				date("d-m-Y",strtotime($row->TANGGAL_SP)),
				$row->KETERANGAN
            );
            $no++;
        }
        if (isset($record_items))
            $this->output->set_output($this->flexigrid->json_build($records['record_count'], $record_items));
        else
            $this->output->set_output('{"page":"1","total":"0","rows":[]}');
	}
	function form_sp($id,$parameter=FALSE){
		$this->load->library("form_validation");
		$this->form_validation->set_error_delimiters('<div class="alert alert-error fade in">', '</div>');
		$this->form_validation->set_rules(array(
		array('field'=>'tgl_sp','label'=>'Tanggal SP','rules'=>'required'),
		array('field'=>'ket','label'=>'Keterangan','rules'=>'required'),
		));
		$this->form_validation->set_message('required','Kolom %s harus diisi !');
		if($this->form_validation->run() == FALSE){
			if($parameter == TRUE){
				$data["value"] = $this->sp_mahasiswa_model->get($parameter);
			}
			$data["parameter"]	= $parameter;
			$data["id"]			= $id;
			$data["active"] = "master";
			$data["content"]	= $this->load->view("form_sp",$data,true);
			$this->load->view("main_new",$data);
		} else {
			$tgl_sp	= $this->input->post("tgl_sp");
			$ket	= $this->input->post("ket");
			$data	= array(
			"ID_MAHASISWA"=>$id,
			"TANGGAL_SP"=>date("Y-m-d",strtotime($tgl_sp)),
			"KETERANGAN"=>$ket
			);
			$this->sp_mahasiswa_model->save($data, $parameter);
			redirect("/sp_mahasiswa/index/" . $id);
		}
	}
	function delete_sp(){
		$ids	= $this->input->post('items');
		$delete	= $this->sp_mahasiswa_model->delete($ids);
		if($delete == TRUE){
			$msg ='Data yang anda pilih berhasil dihapus';
			$this->output->set_output($msg);
		} else {
			$msg ='Gagal menghapus data';
			$this->output->set_output($msg);
		}
	}
}
?>